import facecat from "facecat"
import facecat_ws from "facecat_ws"
/*
* 点击方法
* view:视图
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
* clicks:点击次数
*/
let onClick = function (view, firstTouch, firstPoint, secondTouch, secondPoint, clicks) {
    facecat.onClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks);
};

/*
* 触摸开始方法
* view:视图
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
*/
let onTouchBegin = function (view, firstTouch, firstPoint, secondTouch, secondPoint) {
    facecat.onTouchBeginDefault(view, firstTouch, firstPoint, secondTouch, secondPoint);	
};

/*
* 触摸移动方法
* view:视图
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
*/
let onTouchMove = function (view, firstTouch, firstPoint, secondTouch, secondPoint) {
	facecat.onTouchMoveDefault(view, firstTouch, firstPoint, secondTouch, secondPoint);	
};

/*
* 触摸结束方法
* view:视图
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
*/
let onTouchEnd = function (view, firstTouch, firstPoint, secondTouch, secondPoint) {
	facecat.onTouchEndDefault(view, firstTouch, firstPoint, secondTouch, secondPoint);	
};

let gPaint = new facecat.FCPaint();
let that = null;

// index.js
// 获取应用实例
const app = getApp()
Page({
    data: {
        canvasWidth:0,
        canvasHeight:0,
        facecatStyle:"",
        facecatContent:"",
        facecatFocus:false
      },
      bindInputChange: function(e) {  
        if(gPaint.editingTextBox){
            gPaint.editingTextBox.text = e.detail.value;
            facecat.invalidateView(gPaint.editingTextBox);
            gPaint.editingTextBox = null;
            gPaint.app.setData({
                facecatStyle: "position:absolute;left:0px;top:0px;width:0px;height:0px;display:none;", 
                facecatFocus:false,
                facecatContent:""
             })
        }
      },
  touchStartCanvas: function (e) {
    facecat_ws.handleTouchStart(gPaint, e, onTouchBegin);
  },touchMoveCanvas: function (e) {
    facecat_ws.handleTouchMove(gPaint, e, onTouchMove);
  },touchEndCanvas: function (e) {
    facecat_ws.handleTouchEnd(gPaint, e, onTouchEnd, onClick);
  },
  onLoad() {
      that = this;
    wx.createSelectorQuery().select('#myCanvas').fields({ node: true, size: true }).exec((cres) => {       
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    canvasWidth: res.windowWidth,  
                    canvasHeight: res.windowHeight
                 })
                cres[0].width = res.windowWidth;
                cres[0].height = res.windowHeight;
            }
        });   
        facecat_ws.scaleCanvas(gPaint, cres);
        /*
        * 重绘背景的实现方法
        * view:视图
        * paint:绘图对象
        * clipRect:裁剪区域
        */
        gPaint.onPaint = function (view, paint, clipRect) {
            facecat.onPaintDefault(view, paint, clipRect);
        };            
        /*
        * 重绘边框的实现方法
        * view:视图
        * paint:绘图对象
        * clipRect:裁剪区域
        */
        gPaint.onPaintBorder = function (view, paint, clipRect) {
            facecat.onPaintBorderDefault(view, paint, clipRect);
        };
        /*
        * 更新布局的实现方法
        * views:视图集合
        */
        gPaint.onUpdateView = function (views) {
            facecat.updateViewDefault(views);
        }
        //解析加载XML
        let xml = `<?xml version="1.0" encoding="UTF-8"?>
        <html xmlns="facecat">
          <head></head>
          <body>
            <div dock="fill" type="tab" selectedindex="0">
              <div name="TabPage" text="可视化生成" type="tabpage">
                <tree name="Tree" text="Tree" location="45,56" size="200,200" headervisible="False">
                </tree>
                <tree name="Tree2" text="Tree2" location="256,56" size="200,200">
                  <tr>
                    <th text="FCGridColumn" name="FCGridColumn" width="300"/>
                    <th text="FCGridColumn2" name="FCGridColumn2"/>
                    <th text="FCGridColumn3" name="FCGridColumn3"/>
                  </tr>
                  <nodes>
                    <node text="Node1">
                    <node text="Node2">
                    <node text="Node3"/>
                    </node>
                    </node>
                  </nodes>
                </tree>
              </div>
              <div name="TabPage2" text="代码创建" type="tabpage"/>
            </div>
          </body>
        </html>
        `;
	    facecat_ws.renderFaceCatWeChat(gPaint, xml, that);
        let tree = facecat.findViewByName("Tree", gPaint.views);
        tree.showCheckBox = true;
        let column = new facecat.FCTreeColumn();
        column.width = 500;
        tree.columns.push(column);

        let rootNode = new facecat.FCTreeNode();
        rootNode.value = "证监会行业类";
        facecat.appendTreeNode(tree, rootNode, null);

        let node1 = new facecat.FCTreeNode();
        node1.value = "农、林、牧、渔业";
        facecat.appendTreeNode(tree, node1, rootNode);

        let strs1 = new Array();
        strs1.push("农业");
        strs1.push("林业");
        strs1.push("畜牧业");
        strs1.push("渔业");
        strs1.push("农、林、牧、渔服务业");
        for (let i = 0; i < strs1.length; i++) {
            let subNode1 = new facecat.FCTreeNode();
            subNode1.value = strs1[i];
            facecat.appendTreeNode(tree, subNode1, node1);
        }

        let node2 = new facecat.FCTreeNode();
        node2.value = "采矿业";
        facecat.appendTreeNode(tree, node2, rootNode);

        let strs2 = new Array();
        strs2.push("煤炭开采和洗选业");
        strs2.push("石油和天然气开采业");
        strs2.push("黑色金属矿采选业");
        strs2.push("有色金属矿采选业");
        strs2.push("非金属矿采选业");
        strs2.push("开采辅助活动");
        for (let i = 0; i < strs2.length; i++) {
            let subNode2 = new facecat.FCTreeNode();
            subNode2.value = strs2[i];
            facecat.appendTreeNode(tree, subNode2, node2);
        }

        let node3 = new facecat.FCTreeNode();
        node3.value = "制造业";
        facecat.appendTreeNode(tree, node3, rootNode);

        let strs3 = new Array();
        strs3.push("农副食品加工业");
        strs3.push("食品制造业");
        strs3.push("酒、饮料和精制茶制造业");
        strs3.push("纺织业");
        strs3.push("纺织服装、服饰业");
        strs3.push("皮革、毛皮、羽毛及其制品和制鞋业");
        strs3.push("木材加工和木、竹、藤、棕、草制品业");
        strs3.push("家具制造业");
        strs3.push("造纸和纸制品业");
        strs3.push("印刷和记录媒介复制业");
        strs3.push("文教、工美、体育和娱乐用品制造业");
        strs3.push("石油加工、炼焦和核燃料加工业");
        strs3.push("化学原料及化学制品制造业");
        strs3.push("医药制造业");
        strs3.push("化学纤维制造业");
        strs3.push("橡胶和塑料制品业");
        strs3.push("非金属矿物制品业");
        strs3.push("黑色金属冶炼和压延加工业");
        strs3.push("有色金属冶炼和压延加工业");
        strs3.push("金属制品业");
        strs3.push("通用设备制造业");
        strs3.push("专用设备制造业");
        strs3.push("汽车制造业");
        strs3.push("铁路、船舶、航空航天和其他运输设备制造业");
        strs3.push("电气机械和器材制造业");
        strs3.push("计算机、通信和其他电子设备制造业");
        strs3.push("仪器仪表制造业");
        strs3.push("其他制造业");
        strs3.push("废弃资源综合利用业");
        for (let i = 0; i < strs3.length; i++) {
            let subNode3 = new facecat.FCTreeNode();
            subNode3.value = strs3[i];
            facecat.appendTreeNode(tree, subNode3, node3);
        }
        facecat.invalidate(gPaint);
    })
  }
})
